package dk.au.transxor.mote;

import java.io.IOException;

import net.tinyos.message.Message;
import net.tinyos.message.MessageListener;
import net.tinyos.message.MoteIF;
import net.tinyos.packet.BuildSource;
import net.tinyos.packet.PhoenixSource;
import net.tinyos.util.Messenger;

public class MoteUart implements Messenger {

	public static final String Device = "serial@/dev/ttyUSB0:9600";
	
	private PhoenixSource psource;
	private MoteIF moteif;

	public MoteUart() {
		psource = BuildSource.makePhoenix(BuildSource.makePacketSource(Device), this);
		moteif = new MoteIF(psource);
		
	}

	public void sendMessage(int moteId, Message message) {

		try {

			moteif.send(moteId, message);
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void stop() {
		psource.shutdown();
	}
	
	public void addMessageListener(Message message, MessageListener listener) {
		moteif.registerListener(message, listener);
	}
	
	public void removeMessageListener(Message message, MessageListener listener) {
		moteif.deregisterListener(message, listener);
	}

	// Status messages from the Phoenix source
	@Override
	public void message(String s) {
		System.out.println("Messenger: " + s);
	}
	
}
